using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;

namespace FiltronASP.Models.DAO.WIX
{
    public class WixFilterShapeDAO : Abstracts.AbstractGenericDAO<WixFilterShape, System.Nullable<int>>,
        Interfaces.WIX.IWixFilterShapeDAO
    {
        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "shape_id";
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie typu.
        /// </summary>
        /// <param name="typeId">typ</param>
        /// <returns>rekordy o zadanym typie lub null, gdy nie ma</returns>
        public IQueryable<WixFilterShape> FindByType(int typeId)
        {
            if (typeId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixFilterShape> filterShapes = this._cmsEntitiesDataContext.WixFilterShapes.Where(
                    fish => this._cmsEntitiesDataContext.WixFilterTypeShapes.Where(tysh =>
                        tysh.type_id.Equals(typeId)).Select(ts => ts.shape_id).Contains(fish.shape_id)
                        );
                if (!(filterShapes.Any()))
                {
                    return null;
                }   //if
                return filterShapes;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }
    }
}